from django.db import models
from django.contrib.auth.models import User


class Group(models.Model):
    name = models.CharField(max_length=256)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return self.name
    
    
class GroupShip(models.Model):
    group = models.ForeignKey(Group)
    member = models.ForeignKey(User)
    is_manager = models.BooleanField()
    balance = models.FloatField()
    
    def __unicode__(self):
        return self.group.name + "-" + self.member.username


class ActivityDetail(models.Model):
    group = models.ForeignKey(Group)
    date = models.DateField()
    totoal_fee = models.FloatField()
    remark = models.TextField(blank=True)

    def __unicode__(self):
        return self.group.name + " " + str(self.date) 
        

class ActivitySettlement(models.Model):
    activity = models.ForeignKey(ActivityDetail)    
    groupship = models.ForeignKey(GroupShip)
    # how many people shared in the account
    shares = models.IntegerField()
    # ball money
    offset = models.FloatField()


class AccountDetail(models.Model):
    groupship = models.ForeignKey(GroupShip)
    datetime = models.DateTimeField()
    money = models.FloatField()
    remark = models.CharField(max_length=256, blank=True)    
        
    def save(self, *args, **kwargs):
        super(AccountDetail, self).save(*args, **kwargs)
        self.groupship.balance += self.money
        self.groupship.save()
        